home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_toolbx.arc / SORTNUM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-30  |  1014 b   |  50 lines

  1. /* sortnum.c - sort input numbers - numbers entered form keyboard */
  2. #include "stdio.h"
  3.  
  4. main()
  5.  {
  6.     int i , n , t ;
  7.     int a[100] ;
  8.  
  9.     printf(" Enter numbers - (type q to stop)") ;
  10.     n=0 ;
  11.     while( scanf("%d",&t) != 0 )
  12.       { a[n] = t ;
  13.         n = n + 1 ;
  14.       }
  15.  
  16.     sortn(a,n) ;
  17.  
  18.     for(i=1 ; i < n ; i=i+1 )
  19.       { printf(" %d",a[i] ) ; }
  20.  }
  21.  
  22. sortn(x,nx)    /* put an array of ints into assending order */
  23.  int x[] ;    /* the array */
  24.  int nx  ;    /* count of items to sort */
  25.  {
  26.     int i , j , pick ;
  27.  
  28.     for(i=0 ; i < (nx-1) ; i=i+1 )
  29.       {    /* find the smallest remaining number */
  30.         pick = i ;
  31.         for(j=i+1 ; j < nx ; j=j+1 )
  32.           { if( x[j] < x[pick] )
  33.         pick = j ; /*element x[pick] is smallest so far */
  34.           }
  35.         swap( & x[pick] , & x[i] ) ;  /* exchange - smallest first */
  36.       }
  37.  }
  38.  
  39. int swap(p1,p2)         /* swap two numbers */
  40.  int *p1 ;            /* points to first number */
  41.  int *p2 ;            /* points to second number */
  42.  {
  43.     int temp ;
  44.  
  45.     temp = *p1 ;
  46.     *p1 = *p2  ;
  47.     *p2 = temp ;
  48.  }
  49.  
  50.